﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using Panny.Data;
using System.Data.SqlClient;
using DBUtility;//请先添加引用
namespace Easydp.Models
{
    public class AccountInfoManager
    {
        public static string connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// 
        public int Update(Entities.TT_AccountInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update TT_AccountInfo set ");
            strSql.Append("AgentID=@AgentID,");
            strSql.Append("AccountCode=@AccountCode,");
            strSql.Append("AccountType=@AccountType,");
            strSql.Append("CreateOn=@CreateOn");
            strSql.Append(" where AccountID=@AccountID ");
           

            using (SqlText updation = new SqlText(strSql.ToString()))
            {
                updation.AddParameter("@AccountID", model.AccountID);
                updation.AddParameter("@AgentID", model.AgentID);
                updation.AddParameter("@AccountCode", model.AccountCode);
                updation.AddParameter("@AccountType", model.AccountType);
                updation.AddParameter("@AccountType", model.AccountType);
                updation.AddParameter("@CreateOn", model.CreateOn);
                
                return updation.ExecuteNonQuery();

            }
        }


        /// <summary>
        /// 得到model一个对象实体
        /// </summary>
        /// 
        public Entities.TT_AccountInfo GetModel(int AgentID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AccountID,AgentID,AccountCode,AccountType,CreateOn from TT_AccountInfo ");
            strSql.Append(" where AgentID=@AgentID ");
            SqlParameter[] parameters = {
					new SqlParameter("@AgentID", SqlDbType.Int,4)};
            parameters[0].Value = AgentID;

            Entities.TT_AccountInfo model = new Easydp.Models.Entities.TT_AccountInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["AccountID"].ToString() != "")
                {
                    model.AccountID = int.Parse(ds.Tables[0].Rows[0]["AccountID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AgentID"].ToString() != "")
                {
                    model.AgentID = int.Parse(ds.Tables[0].Rows[0]["AgentID"].ToString());
                }
                model.AccountCode = ds.Tables[0].Rows[0]["AccountCode"].ToString();
                model.AccountType = ds.Tables[0].Rows[0]["AccountType"].ToString();
                if (ds.Tables[0].Rows[0]["CreateOn"].ToString() != "")
                {
                    model.CreateOn = DateTime.Parse(ds.Tables[0].Rows[0]["CreateOn"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }




        /// <summary>
        /// 获得dataset数据列表
        /// </summary>
        public DataSet GetDataset(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AccountID,AgentID,AccountCode,AccountType,CreateOn ");
            strSql.Append(" FROM TT_AccountInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 得到list列表
        /// </summary>
        /// 
        public List<Entities.TT_AccountInfo> GetList(int AgentID)
        {
            List<Entities.TT_AccountInfo> list = new List<Easydp.Models.Entities.TT_AccountInfo>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select   AccountID,AgentID,AccountCode,AccountType,CreateOn from TT_AccountInfo ");
            strSql.Append(" where AgentID=@AgentID ");
            using (SqlText selectlist = new SqlText(strSql.ToString()))
            {
                selectlist.AddParameter("@AgentID", AgentID);
                while (selectlist.Read())
                {
                    Entities.TT_AccountInfo model = new Easydp.Models.Entities.TT_AccountInfo();
                    if (selectlist["AccountID"].ToString() != "")
                    {
                        model.AccountID = int.Parse(selectlist["AccountID"].ToString());
                    }
                    if (selectlist["AgentID"].ToString() != "")
                    {
                        model.AgentID = int.Parse(selectlist["AgentID"].ToString());
                    }
                    model.AccountCode = selectlist["AccountCode"].ToString();
                    model.AccountType = selectlist["AccountType"].ToString();
                    if (selectlist["CreateOn"].ToString() != "")
                    {
                        model.CreateOn = DateTime.Parse(selectlist["CreateOn"].ToString());
                    }
                    list.Add(model);
                }
            }
            return list;
        }

        /// <summary>
        ///  通过帐户类型
        /// </summary>
        /// <param name="?"></param>
        ///  <param name="?"></param>
        /// <returns></returns>
        public Entities.TT_AccountInfo GetAccountByAccountType(string accountType,string agentID)
        {
            Entities.TT_AccountInfo accountObj = new Easydp.Models.Entities.TT_AccountInfo();
            using (SqlText selection = new SqlText(" SELECT * FROM TT_AccountInfo WHERE AccountType=@AccountType AND AgentID = @AgentID"))
            {
                selection.AddParameter("@AgentID", agentID);
                selection.AddParameter("@AccountType", accountType);
                while (selection.Read())
                {
                    accountObj.AccountCode = selection["AccountCode"].ToString();
                    accountObj.AccountType = selection["AccountType"].ToString();
                    accountObj.AgentID = Convert.ToInt32(selection["AgentID"].ToString());
                }
            }
            return accountObj;
        }

    }
}
